﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Text;
using System.IO;
using System.Threading.Tasks;

public class Menu
{
    public static int StartMenu()
    {
        Console.CursorVisible = false;
        Console.ForegroundColor = ConsoleColor.Green;
        byte[,] bigMenu = {
            {1,0,0,0,0,0,1,0,1,1,1,1,1,0,1,0,0,0,0,1,0,1,0,0,0,0,1},
            {1,1,0,0,0,1,1,0,1,0,0,0,0,0,1,0,0,0,0,1,0,1,0,0,0,0,1},
            {1,0,1,0,1,0,1,0,1,0,0,0,0,0,1,1,0,0,0,1,0,1,0,0,0,0,1},
            {1,0,0,1,0,0,1,0,1,1,1,1,1,0,1,0,1,0,0,1,0,1,0,0,0,0,1},
            {1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,0,1,0,1,0,1,0,0,0,0,1},
            {1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,0,0,1,1,0,1,0,0,0,0,1},
            {1,0,0,0,0,0,1,0,1,1,1,1,1,0,1,0,0,0,0,1,0,0,1,1,1,1,0},
            };
        
        Console.SetCursorPosition(45, 13);
        Console.WriteLine(" \"SHE HULK\" - TEAM PROJECT");
        char fullBox = '\u2588';
        for (int i = 0; i < 7; i++)
        {
            for (int g = 0; g < 27; g++)
            {
                if (bigMenu[i, g] == 1)
                {
                    Console.SetCursorPosition(g + 45, i + 15);
                    Console.Write(fullBox);
                }
            }
        }
        RimDraw();
        return MenuChanges(0);
    }

    public static int LevelMenu(int currLevel)
    {
        return MenuChanges(1);
    }

    static int MenuOptions(int option)
    {
        if (Console.KeyAvailable)
        {
            ConsoleKeyInfo opt = Console.ReadKey();
            if (opt.Key == ConsoleKey.UpArrow) option--;
            else if (opt.Key == ConsoleKey.DownArrow) option++;
            else if (opt.Key == ConsoleKey.Enter) return 10;
        }
        return option;
    }

    static int MenuChanges(int menuType)
    {
        int option = 0;
        string[] menuOptions = {"S T A R T", "C O N T R O L S", "L E V E L", "H I G H   S C O R E S", "H E L P", "E X I T"};
        string[] levelOptions = { "L E V E L 1", "L E V E L 2", "L E V E L 3", "L E V E L 4", "L E V E L 5", "L E V E L 6" };
        
        if (menuType == 1)
        {
            Array.Copy(levelOptions, menuOptions, levelOptions.Length);
            Console.Clear();
            RimDraw();
            LabelLevelMenu();
        }
        while (true)
        {   
            for (int i = 0; i < menuOptions.Length; i++)
            {
                int xPosition = 45 + (27 - menuOptions[i].Length) / 2;
                Console.SetCursorPosition(xPosition, 30 + i * 4);
                if (option == i)
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine(menuOptions[i]);
                    Console.ForegroundColor = ConsoleColor.Gray;
                    continue;
                }
                Console.WriteLine(menuOptions[i]);
                int checkOption = MenuOptions(option);
                if (checkOption == 10) return option;
                option = checkOption;
                if (option == menuOptions.Length) option = 0;
                else if (option == -1) option = menuOptions.Length - 1;
            }
        }
    }

    static void RimDraw()
    {
        Console.ForegroundColor = ConsoleColor.Green;
        char rim = '\u2591';
        for (int i = 0; i < 36; i++)
        {
            Console.SetCursorPosition(40 + i, 10);
            Console.WriteLine(rim);
        }
        for (int i = 0; i < 36; i++)
        {
            Console.SetCursorPosition(40 + i, 55);
            Console.WriteLine(rim);
        }
        for (int i = 0; i < 44; i++)
        {
            Console.SetCursorPosition(40, 11 + i);
            Console.WriteLine(rim);
        }
        for (int i = 0; i < 44; i++)
        {
            Console.SetCursorPosition(75, 11 + i);
            Console.WriteLine(rim);
        }
    }

    public static void LabelLevelMenu()
    {
        Console.CursorVisible = false;
        Console.ForegroundColor = ConsoleColor.Green;
        byte[,] bigMenu = {
            {1,0,0,0,0,1,1,1,1,0,1,0,0,0,0,1,0,1,1,1,1,0,1,0,0,0,0},
            {1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,1,0,0,0,0,1,0,0,0,0},
            {1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,1,0,0,0,0,1,0,0,0,0},
            {1,0,0,0,0,1,1,1,1,0,1,0,0,0,0,1,0,1,1,1,1,0,1,0,0,0,0},
            {1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,1,0,0,0,0,1,0,0,0,0},
            {1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0,0,1,0,0,0,0},
            {1,1,1,1,0,1,1,1,1,0,0,0,1,1,0,0,0,1,1,1,1,0,1,1,1,1,0},
            };

        Console.SetCursorPosition(45, 13);
        Console.WriteLine(" \"SHE HULK\" - TEAM PROJECT");
        char fullBox = '\u2588';
        for (int i = 0; i < 7; i++)
        {
            for (int g = 0; g < 27; g++)
            {
                if (bigMenu[i, g] == 1)
                {
                    Console.SetCursorPosition(g + 45, i + 15);
                    Console.Write(fullBox);
                }
            }
        }
    }

    public static void ScoreRecord(int currScore, int time)
    {
        DateTime date = DateTime.Now; // using datetime :)
        string initials = string.Empty;
        while (initials.Length != 3)
        {
            RimDraw();
            Console.SetCursorPosition(46, 18);
            Console.WriteLine("Y O U R   S C O R E   IS");
            Console.SetCursorPosition(50, 22);
            Console.WriteLine("{0}    P O I N T S", currScore);
            Console.SetCursorPosition(44, 33);
            Console.WriteLine("E N T E R   3  S Y M B O L S");
            Console.SetCursorPosition(44, 35);
            Console.WriteLine("F O R   T H E   R E C O R D:");
            Console.SetCursorPosition(57, 38);
            Console.CursorVisible = true;
            initials = string.Empty;
            initials = Console.ReadLine();
            initials = initials.ToUpper();
            Console.CursorVisible = false;
            
            if (initials.Length != 3)
            {
                Console.SetCursorPosition(45, 40);
                Console.WriteLine("I N V A L I D   I N P U T!");
                Thread.Sleep(3000);
            }
            Console.Clear();
        }
        try
        {
            var writer = new StreamWriter("results.txt",true);
            {
                using (writer)
                {
                    writer.WriteLine("{1}#{2}#{3}#{0:D5}", currScore, initials, time, date);
                }
            }
        }
        catch (AccessViolationException)
        {
            Console.WriteLine("HIGH SCORES DATA TEMPORARY UNREACHABLE.");
        }
        catch (Exception)
        {
            Console.WriteLine("COULD NOT RECORD RESULTS.TRY LATER!");
        }
    }
    
    public static void HighScores()
    {
        Console.Clear();
        List<string[]> ranking = new List<string[]>();
        try
        {
            var reader = new StreamReader("results.txt");
            {
                using (reader)
                {
                    string line = reader.ReadLine();
                    while (line != null)
                    {
                        ranking.Add(line.Split(new char[] {'#'},StringSplitOptions.RemoveEmptyEntries).ToArray());
                        line = reader.ReadLine();
                    }
                }
            }
        }
        catch (Exception)
        {
            Console.WriteLine("HIGH SCORES DATA TEMPORARY UNREACHABLE.");
        }
        var sortedScores = ranking.OrderByDescending(x => int.Parse(x[3])).ToArray();

        Console.ForegroundColor = ConsoleColor.Red;
        Console.SetCursorPosition(30,30);
        Console.Write("NAME      PLAYED TIME      DATE                       SCORE");
        for (int i = 0; i < Math.Min(5,sortedScores.Length); i++)
        {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.SetCursorPosition(30, 34 + i * 5);
            Console.WriteLine("{0}".PadLeft(0) + "{1}".PadLeft(10) + "{2}".PadLeft(16) + "{3}".PadLeft(6)
                , sortedScores[i][0], sortedScores[i][1], sortedScores[i][2], sortedScores[i][3]);
        }
        //Console.WriteLine("H I G H   S C O R E S");

        Console.CursorVisible = false;
        Console.ForegroundColor = ConsoleColor.Green;
        byte[,] bigMenu = {
            {0,1,1,0,1,1,1,1,0,1,1,1,1,0,1,1,1,0,0,1,1,1,1,0,0,1,1},
            {1,0,0,0,1,0,0,0,0,1,0,0,1,0,1,0,0,1,0,1,0,0,0,0,1,0,0},
            {1,0,0,0,1,0,0,0,0,1,0,0,1,0,1,0,0,1,0,1,0,0,0,0,1,0,0},
            {0,1,1,0,1,0,0,0,0,1,0,0,1,0,1,1,1,0,0,1,1,1,1,0,0,1,1},
            {0,0,1,0,1,0,0,0,0,1,0,0,1,0,1,1,0,0,0,1,0,0,0,0,0,0,1},
            {0,0,1,0,1,0,0,0,0,1,0,0,1,0,1,0,1,0,0,1,0,0,0,0,0,0,1},
            {1,1,0,0,1,1,1,1,0,1,1,1,1,0,1,0,0,1,0,1,1,1,1,0,1,1,0},
            };

        Console.SetCursorPosition(45, 13);
        Console.WriteLine(" \"SHE HULK\" - TEAM PROJECT");
        char fullBox = '\u2588';
        for (int i = 0; i < 7; i++)
        {
            for (int g = 0; g < 27; g++)
            {
                if (bigMenu[i, g] == 1)
                {
                    Console.SetCursorPosition(g + 45, i + 15);
                    Console.Write(fullBox);
                }
            }
        }

        Console.SetCursorPosition(48, 20);
        System.Threading.Thread.Sleep(5000);
        Console.Clear();
    }

    private static ConsoleKey setControls()
    {
        while (true)
        {
            if (Console.KeyAvailable)
            {
                ConsoleKeyInfo key = Console.ReadKey(true);
                return key.Key;
            }
        }
    }
    public static void Controls()
    {
        Console.Clear();
        RimDraw();
        Console.CursorVisible = false;
        Console.ForegroundColor = ConsoleColor.Green;
        byte[,] bigMenu = {
            {1,1,1,0,1,1,1,0,1,0,0,1,0,1,1,1,0,1,1,1,0,1,1,1,0,1,0,0,0,0,1,1},
            {1,0,0,0,1,0,1,0,1,0,0,1,0,0,1,0,0,1,0,1,0,1,0,1,0,1,0,0,0,1,0,0},
            {1,0,0,0,1,0,1,0,1,1,0,1,0,0,1,0,0,1,0,1,0,1,0,1,0,1,0,0,0,1,0,0},
            {1,0,0,0,1,0,1,0,1,0,1,1,0,0,1,0,0,1,1,1,0,1,0,1,0,1,0,0,0,0,1,0},
            {1,0,0,0,1,0,1,0,1,0,0,1,0,0,1,0,0,1,1,0,0,1,0,1,0,1,0,0,0,0,0,1},
            {1,0,0,0,1,0,1,0,1,0,0,1,0,0,1,0,0,1,0,1,0,1,0,1,0,1,0,0,0,0,0,1},
            {1,1,1,0,1,1,1,0,1,0,0,1,0,0,1,0,0,1,0,1,0,1,1,1,0,1,1,1,0,1,1,0},
            };

        Console.SetCursorPosition(45, 13);
        Console.WriteLine(" \"SHE HULK\" - TEAM PROJECT");
        char fullBox = '\u2588';
        for (int i = 0; i < 7; i++)
        {
            for (int g = 0; g < 32; g++)
            {
                if (bigMenu[i, g] == 1)
                {
                    Console.SetCursorPosition(g + 42, i + 15);
                    Console.Write(fullBox);
                }
            }
        }

        Console.SetCursorPosition(43, 30);
        Console.WriteLine("Move left       [{0}]",Game.MoveLeft);
        Console.SetCursorPosition(43, 32);
        Console.WriteLine("Move right      [{0}]",Game.MoveRight);
        Console.SetCursorPosition(43, 34);
        Console.WriteLine("Move up         [{0}]",Game.MoveUp);
        Console.SetCursorPosition(43, 36);
        Console.WriteLine("Move down       [{0}]",Game.MoveDown);
        Console.SetCursorPosition(43, 38);
        Console.WriteLine("Shoot           [{0}]",Game.Shoot);
        Console.SetCursorPosition(43, 40);
        Console.WriteLine("Pause           [{0}]",Game.Pause);
        Console.SetCursorPosition(43, 42);
        Console.WriteLine("Press [C] to change controls");
        Console.SetCursorPosition(43, 52);
        Console.WriteLine("Press  [backspace] to return.");

        while (true)
        {
            if (Console.KeyAvailable)
            {
                ConsoleKeyInfo pressedKey = Console.ReadKey(true);
                if (pressedKey.Key == ConsoleKey.Backspace)
                {
                    Console.Clear();
                    return;
                }
                if(pressedKey.Key==ConsoleKey.C)
                {
                    Console.SetCursorPosition(43, 46);
                    Console.WriteLine("Set move left key");
                    Game.MoveLeft = setControls();
                    Console.SetCursorPosition(43, 46);
                    Console.WriteLine("Set move right key");
                    Game.MoveRight = setControls();
                    Console.SetCursorPosition(43, 46);
                    Console.WriteLine("Set move up key");
                    Game.MoveUp = setControls();
                    Console.SetCursorPosition(43, 46);
                    Console.WriteLine("Set move down key");
                    Game.MoveDown = setControls();
                    Console.SetCursorPosition(43, 46);
                    Console.WriteLine("Set shoot key");
                    Game.Shoot = setControls();
                    Controls();
                    return;
                }
            }
        }
    }

    public static void Help()//krum
    {
        Console.Clear();
        RimDraw();
        Console.CursorVisible = false;
        Console.ForegroundColor = ConsoleColor.Green;
        byte[,] bigMenu = {
            {0,0,0,0,1,0,0,1,0,1,1,1,1,0,1,0,0,0,0,1,1,1,0,0,0,0,0},
            {0,0,0,0,1,0,0,1,0,1,0,0,0,0,1,0,0,0,0,1,0,0,1,0,0,0,0},
            {0,0,0,0,1,0,0,1,0,1,0,0,0,0,1,0,0,0,0,1,0,0,1,0,0,0,0},
            {0,0,0,0,1,1,1,1,0,1,1,1,1,0,1,0,0,0,0,1,1,1,0,0,0,0,0},
            {0,0,0,0,1,0,0,1,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0},
            {0,0,0,0,1,0,0,1,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0},
            {0,0,0,0,1,0,0,1,0,1,1,1,1,0,1,1,1,1,0,1,0,0,0,0,0,0,0},
            };

        Console.SetCursorPosition(45, 13);
        Console.WriteLine(" \"SHE HULK\" - TEAM PROJECT");
        char fullBox = '\u2588';
        for (int i = 0; i < 7; i++)
        {
            for (int g = 0; g < 27; g++)
            {
                if (bigMenu[i, g] == 1)
                {
                    Console.SetCursorPosition(g + 45, i + 15);
                    Console.Write(fullBox);
                }
            }
        }

        Console.SetCursorPosition(43, 30);
        Console.WriteLine("Take control  over the  mighty");
        Console.SetCursorPosition(43, 32);
        Console.WriteLine("She  Hulk  and smash  all your");
        Console.SetCursorPosition(43, 34);
        Console.WriteLine("enemies   with   heavy  rocks!");
        Console.SetCursorPosition(43, 37);
        Console.WriteLine("Objective: Destroy all enemies");
        Console.SetCursorPosition(43, 40);
        Console.WriteLine("Hint:  Killing the coordinator");
        Console.SetCursorPosition(43, 42);
        Console.WriteLine("will  cause  enemies  to  flee");
        Console.SetCursorPosition(43, 44);
        Console.WriteLine("to  the  center  of  the field");
        Console.SetCursorPosition(43, 46);
        Console.WriteLine("but be ware since he respawns!");
        Console.SetCursorPosition(43, 52);
        Console.WriteLine("Press  [backspace] to return.");

        while (true)
        {
            if (Console.KeyAvailable)
            {
                ConsoleKeyInfo pressedKey = Console.ReadKey(true);
                if (pressedKey.Key == ConsoleKey.Backspace)
                {
                    Console.Clear();
                    return;
                }
            }
        }
    }
}